package cn.homework.web.servlet;

import cn.homework.domain.Customers;
import cn.homework.domain.PageBean;
import cn.homework.service.BackstageCustomerService;
import cn.homework.service.impl.BackstageCustomerServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/backstageCustomerServlet")
public class BackstageCustomerServlet extends HttpServlet {
    private BackstageCustomerService service = new BackstageCustomerServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
//        System.out.println(demand);
        switch (demand) {
            case "show": {
                backstageCustomerShow(request, response);
                break;
            }
            case "details": {
                backstageCustomerDetails(request, response);
                break;
            }
            case "delete": {
                backstageCustomerDelete(request, response);
                break;
            }
            case "query": {
                backstageCustomerQuery(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 显示所有的用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void backstageCustomerShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Customers> listQueryCustomers = service.customersShow();
        HttpSession session = request.getSession();
        session.setAttribute("listQueryCustomers", listQueryCustomers);
        response.sendRedirect(request.getContextPath() + "/BackstageHome.jsp");
//        request.getRequestDispatcher("/BackstageHome.jsp").forward(request, response);
    }

    /**
     * 用户的详细资料
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void backstageCustomerDetails(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String customerName = request.getParameter("customerName");
        Customers customer = service.findCustomerByName(customerName);
        HttpSession session = request.getSession();
        session.setAttribute("customerDetails", customer);
        response.sendRedirect(request.getContextPath() + "/CommodityCustomerDetails.jsp");
    }

    /**
     * 删除所选用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void backstageCustomerDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String customerName = request.getParameter("customerName");
        service.delCustomerByName(customerName);
        backstageCustomerShow(request, response);
    }

    /**
     * 根据条件查询用户
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void backstageCustomerQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<Customers> listQueryCustomers = service.queryCustomers(map);
        HttpSession session = request.getSession();
        session.setAttribute("listQueryCustomers", listQueryCustomers);
        response.sendRedirect(request.getContextPath() + "/BackstageQueryCustomer.jsp");
//        request.getRequestDispatcher("/BackstageQueryCustomer.jsp").forward(request, response);
    }
}
